
package cn.zlg.common.datadump.convertor;

import cn.zlg.common.datadump.annotation.DataDumpField;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public interface FieldValueConvert<E, I> {

    /**
     * 将导入字段的值转换为entity属性的值
     * 
     * @param bean
     *                      导入对象
     * @param value
     *                      字段值
     * @param dataField
     *                      字段定义
     * @return entity对应的值
     */
    I convert(Object bean, E value, DataDumpField dataField);

    default boolean supportBatch() {
        return false;
    }

    default List<I> convert(List<Object> bean, List<E> value, DataDumpField dataDumpField) {
        if(!supportBatch()) {
            throw new UnsupportedOperationException("不支持批量操作");
        }
        if(value == null || value.isEmpty()) {
            return Collections.EMPTY_LIST;
        }
        List<I> result = new ArrayList<>(value.size());
        for(int i=0;i<value.size();i++) {
            result.add(convert(bean.get(i),value.get(i),dataDumpField));
        }
        return result;
    }
}
